package weaver.formmode.customjavacode.customsearch;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: wkj
 * Date: 2020/3/20
 * Time: 10:45
 * Description: 线下日报查看权限控制
 */
public class CustomSearchXXRBQXKZ extends AbstractCustomSqlConditionJavaCode {
    @Override
    public String generateSqlCondition(Map<String, Object> param) throws Exception {
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        RecordSet recordSet = new RecordSet();

        User user = (User) param.get("user");
        int userid = user.getUID();
        baseBean.writeLog("CustomSearchXXRBQXKZ——userid:" + userid);

        String sqlCondition = " (1=0 ";

        //线下考勤权限表
        String XXKQQXTablename = baseBean.getPropValue("Mode_Qxkz", "XXKQQXTablename");
        //线下考勤特殊类权限
        String XXKQTSLQXTablename = baseBean.getPropValue("Mode_Qxkz", "XXKQTSLQXTablename");
        //总部权限
        String ZBQXTablename = baseBean.getPropValue("Mode_Qxkz", "ZBQXTablename");

        boolean flag = false;
        rs.execute("select * from " + ZBQXTablename + " where ry=" + userid);
        if (rs.next()) {
            flag = true;
        }

        if (user.getUID() == 1 || flag) {
            //系统管理员和总部权限能看到全部数据
            sqlCondition = " 1=1 ";
            baseBean.writeLog("CustomSearchXXRBQXKZ——sqlCondition:" + sqlCondition);
            return sqlCondition;
        } else {
            //遍历  线下考勤权限表
            rs.execute("select * from " + XXKQQXTablename + " where bmfzr='" + userid + "'");
            if (rs.next()) {
                String deptid = Util.null2String(rs.getString("deptid"));
                sqlCondition += " or t1.bm in (select id from hrmdepartment start with id='" + deptid + "' connect by" +
                        " prior id= supdepid) ";
            }

            //遍历  线下考勤特殊类权限
            rs.execute("select * from " + XXKQTSLQXTablename + " where ry='" + userid + "'");
            if (rs.next()) {
                String bm = Util.null2String(rs.getString("bm"));
                sqlCondition += " or t1.bm in (" + bm + ")";
            }
        }
        sqlCondition += ") ";
        baseBean.writeLog("CustomSearchXXRBQXKZ——sqlCondition:" + sqlCondition);
        return sqlCondition;
    }
}
